package LeetCode;

import java.util.*;

public class Code78_subsets {

    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        if (nums == null || nums.length == 0) {
            return res;
        }
        dfs(nums, 0, new ArrayDeque<>());
        return res;
    }

    private void dfs(int[] nums, int index, Deque<Integer> path) {
        res.add(new ArrayList<>(path));
        for (int i = index; i < nums.length; i++) {
            path.addLast(nums[i]);
            dfs(nums, i + 1, path);
            path.removeLast();
        }
    }

    public static void main(String[] args) {
        Code78_subsets code78_subsets = new Code78_subsets();
        int[] nums = {1,2,3};
        List<List<Integer>> res = code78_subsets.subsets(nums);

        Iterator it = res.iterator();
        while (it.hasNext()){
            List<Integer> temp = (List<Integer>) it.next();
            for (int j = 0; j < temp.size(); j++) {
                System.out.print(temp.get(j) + " ");
            }
            temp.clear();
            System.out.println();
        }
    }
}
